package net.dgg.dggcloud.service.jrcrm.adviser.widelyresource;

import net.dgg.dggcloud.core.idwork.KeyWorker;
import net.dgg.dggcloud.core.minidao.hibernate.dao.IGenericBaseCommonDao;
import net.dgg.dggcloud.core.util.StringUtils;
import net.dgg.dggcloud.service.entity.user.User;
import net.dgg.dggcloud.service.jrcrm.entity.customer.WidelyResourceAllotRecord;
import net.dgg.dggcloud.service.jrcrm.service.customer.WidelyResourceRecordService;
import net.dgg.dggcloud.service.service.user.UserService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * Created by Administrator on 2018/1/5.
 */
@Service
@Aspect
public class WidelyResourceAdviser {
    @Resource(name = "miniDaoHiberCommonDao")
    protected IGenericBaseCommonDao repository;

    @Resource
    private WidelyResourceRecordService widelyResourceRecordService;

    @Autowired
    protected KeyWorker keyWorker;

    @Resource
    private UserService userService;

    /**
     * 分配后的切点
     */
    @Pointcut("execution(* net.dgg.dggcloud.service.jrcrm.service.customer.WidelyResourceService.updateWidelyResourceReceiver(..))")
    public void allowtAfterAspect(){}


//    @Before("allowtAfterAspect()")
//    public void editBefore(JoinPoint jp) throws CloneNotSupportedException {
//        System.out.print("========");
//    }

    /**
     * 分配完成后执行
     */
    @AfterReturning("allowtAfterAspect()")
    public void addWidelyAllowtedRecord(JoinPoint jp){
        Object[] args = jp.getArgs();
        Map map = (Map)args[0];
        User user = (User) map.get("User");

        User receiver = userService.getUserById(Long.parseLong(map.get("receiverId").toString()));

        WidelyResourceAllotRecord widelyResourceAllotRecord = new WidelyResourceAllotRecord();
        widelyResourceAllotRecord.setId(keyWorker.nextId());

        String resourceMonth = map.get("month").toString();
        StringBuilder monthSb = new StringBuilder(resourceMonth).insert(4,"-");

        widelyResourceAllotRecord.setResourceTime(monthSb.toString());
        widelyResourceAllotRecord.setAllotTime(new Date());
        widelyResourceAllotRecord.setAllotorId(user.getId());
        widelyResourceAllotRecord.setAllotorName(user.getName() + user.getLoginName());
        widelyResourceAllotRecord.setReceiverId(receiver.getId());
        widelyResourceAllotRecord.setReceiverName(receiver.getName()+receiver.getLoginName());
        widelyResourceAllotRecord.setReceiverOrgId(receiver.getOrgId());
        widelyResourceAllotRecord.setTotalNum((Integer) map.get("total"));
        widelyResourceAllotRecord.setFailedNum((Integer) map.get("failed"));
        widelyResourceAllotRecord.setSuccessNum((Integer) map.get("succeed"));
        widelyResourceAllotRecord.setCreaterOrgId(user.getOrgId());
        widelyResourceAllotRecord.setUpdaterOrgId(user.getOrgId());
        widelyResourceRecordService.saveOneRecord(widelyResourceAllotRecord);
    }
}
